package GraphX

import org.apache.log4j.{Level, Logger}
import org.apache.spark.graphx.{Edge, Graph}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object GraphXHomework {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org").setLevel(Level.ERROR)
    val conf: SparkConf = new SparkConf().setAppName(this.getClass.getName).setMaster("local[*]")
    val ssc = new SparkContext(conf)


    //定义顶点
    val vertexArray: Array[(Long, String)] = Array((1L, "SFO"), (2L, "ORD"), (3L, "DFW"))
    val vertexRdd: RDD[(Long, String)] = ssc.makeRDD(vertexArray)
    //定义边
    val edgeArray: Array[Edge[Int]] = Array(Edge(1L, 2L, 1800), Edge(2L, 3L, 800), Edge(3L, 1L, 1400))
    val edgeRDD: RDD[Edge[Int]] = ssc.makeRDD(edgeArray)
    //构造图
    val graph: Graph[String, Int] = Graph(vertexRdd, edgeRDD)


    //获得所有顶点
    println("所有顶点：")
    graph.vertices.foreach(println)
    //获得所有边
    println("================================")
    println("所有边：")
    graph.edges.foreach(println)
    //获得所有triplets
    println("================================")
    println("所有triplets：")
    graph.triplets.foreach(println)
    //获得所有顶点数
    println("================================")
    println("所有顶点数：")
    println(graph.vertices.count())
    //获得边数
    println("================================")
    println("边数：")
    println(graph.edges.count())



    //求机场距离大于1000的有几个，有哪些
    println("================================")
    println("求机场距离大于1000的有哪些：")
    graph.triplets.filter(_.attr>1000).foreach(println)

    //按所有机场之间的距离排序（降序），输出结果
    println("================================")
    println("所有机场之间的距离排序（降序）：")
    graph.edges.sortBy(_.attr).foreach(println)




    ssc.stop()
  }
}
